package com.jeff.android.lab.architecture.mmkv;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.android.lab.architecture.arouter.bean.UserInfo;
import com.jeff.android.lab.bugs.toast.repair.ToastUtil;
import com.tencent.mmkv.MMKV;

import butterknife.OnClick;

/**
 * @author: dingfeng
 * @date: 2020-10-30
 * @since: 1.0.0
 **/
public class MmkvFragment extends BaseFragment {

    @Override
    protected int getRootLayout() {
        return R.layout.architectur_fragment_mmkv;
    }

    @OnClick(R.id.btn_save)
    public void saveData() {
//        MMKV mk = MMKV.defaultMMKV();
//
//        mk.encode("name", "Jack");
//        mk.encode("man", true);
//        mk.encode("age", 30);
//        mk.encode("id", 320683198911125192L);
//        mk.encode("money", 1000000000.88);
//
//        UserInfo userInfo = new UserInfo("dingfeng", 30);
//        mk.encode("user", userInfo);

        MmkvUtil.putString("name", "Jack.D");

        MmkvUtil.putBoolean("man", true);
        MmkvUtil.putInt("age", 30);
        MmkvUtil.putLong("id", 320683198911125192L);
        MmkvUtil.putDouble("money", 1000000000.88);

        UserInfo userInfo = new UserInfo("dingfeng", 30);
        MmkvUtil.putParcelable("user", userInfo);
    }

    @OnClick(R.id.btn_read)
    public void readData() {
//        String name = MMKV.defaultMMKV().decodeString("name", "--");
//        Boolean man = MMKV.defaultMMKV().decodeBool("man", false);
//        int age = MMKV.defaultMMKV().decodeInt("age", 18);
//        float id = MMKV.defaultMMKV().decodeFloat("id", 0L);
//        Double money = MMKV.defaultMMKV().decodeDouble("money", .0);
//        UserInfo user = MMKV.defaultMMKV().decodeParcelable("user", UserInfo.class);

        String name = MmkvUtil.getString("name", "--");
        Boolean man = MmkvUtil.getBoolean("man", false);
        int age = MmkvUtil.getInt("age", 18);
        float id = MmkvUtil.getFloat("id", 0L);
        Double money = MmkvUtil.getDouble("money", .0);
        UserInfo user = MmkvUtil.getParcelable("user", UserInfo.class);

        ToastUtil.show(mContext, name + "\n" + man + "\n" + age + "\n" + id + "\n" + money + "\n" + user.toString());

        MmkvUtil.getDefault().removeValueForKey("name");

        MmkvUtil.getDefault().remove("name");
    }

    @OnClick(R.id.btn_use_crypt)
    public void useCrypt() {
//        String cryptKey = "My-Encrypt-Key";
//        MMKV mk = MMKV.mmkvWithID("iyunbao", MMKV.MULTI_PROCESS_MODE, cryptKey);
//
//        mk.encode("encrypt_key", "Jack.D");
//
//        ToastUtil.show(mContext, "value = " + mk.decodeString("encrypt_key"));

        UserInfo userInfo = new UserInfo("Jack", 18);

        MmkvUtil.putParcelable("encrypt_key", userInfo, true);

        MmkvUtil.putString("user_name", "13951660353", true);

//        ToastUtil.show(mContext, "value = " + MmkvUtil.getParcelable("encrypt_key", UserInfo.class, true));

        ToastUtil.show(mContext, "value = " + MmkvUtil.getString("user_name", ""));

        MmkvUtil.getCryptMMKV().removeValueForKey("user_name");
    }

}
